using App;
using App.Interface;
using Data.Repositorios;
using Domain.Interfaces.Repositorios;
using Domain.Interfaces.Servicos;
using Domain.Servicos;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(SiteCredivale.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(SiteCredivale.App_Start.NinjectWebCommon), "Stop")]

namespace SiteCredivale.App_Start
{
    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    using Ninject;
    using Ninject.Web.Common;
    using System;
    using System.Web;

    public static class NinjectWebCommon
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind(typeof(IAppServiceBase<>)).To(typeof(AppServiceBase<>));
            kernel.Bind<IVW_menuAppService>().To<VW_menuAppService>();
            kernel.Bind<IVW_noticiasAppService>().To<VW_noticiasAppService>();
            kernel.Bind<IConsultasAppService>().To<ConsultasAppService>();
            kernel.Bind<Iformularios_email_AppService>().To<formularios_email_AppService>();
            kernel.Bind<IUsuarioAppService>().To<UsuarioAppService>();
            kernel.Bind<IAvisoAppService>().To<AvisoAppService>();
            kernel.Bind<IDocumentosAppService>().To<DocumentosAppService>();
            kernel.Bind<IAfastamentoAppService>().To<AfastamentoAppService>();
            kernel.Bind<ISolicitacaoAppService>().To<SolicitacaoAppService>();

            kernel.Bind(typeof(IServiceBase<>)).To(typeof(ServiceBase<>));
            kernel.Bind<IVW_menuService>().To<VW_menuService>();
            kernel.Bind<IVW_noticiasService>().To<VW_noticiasService>();
            kernel.Bind<IConsultasService>().To<ConsultasService>();
            kernel.Bind<Ifomularios_emailService>().To<formularios_emailService>();
            kernel.Bind<IUsuarioService>().To<UsuarioService>();
            kernel.Bind<IAvisoService>().To<AvisoService>();
            kernel.Bind<IDocumentosService>().To<DocumentosService>();
            kernel.Bind<IAfastamentoService>().To<AfastamentoService>();
            kernel.Bind<ISolicitacaoService>().To<SolicitacaoService>();

            kernel.Bind(typeof(IRepositoryBase<>)).To(typeof(RepositoryBase<>));
            kernel.Bind<IVW_menuRepository>().To<VW_menuRepository>();
            kernel.Bind<IVW_noticiasRepository>().To<VW_noticiasRepository>();
            kernel.Bind<IConsultasRepository>().To<ConsultasRepository>();
            kernel.Bind<IUsuarioRepository>().To<UsuarioRepository>();
            kernel.Bind<IAvisoRepository>().To<AvisoRepository>();
            kernel.Bind<IDocumentosRepository>().To<DocumentosRepository>();
            kernel.Bind<IAfastamentoRepository>().To<AfastamentoRepository>();
            kernel.Bind<ISolicitacaoRepository>().To<SolicitacaoRepository>();


        }
    }
}